import {
  getCompanyID,
  getUserDepID,
  requestFunCompon,
} from '@/utils';
import { message } from 'antd';
import { useMemo } from 'react';
import { useRequest } from 'umi';

const useManageService = (curRes: any) => {
  const requestTree = useRequest(
    () => {
      let url = '';
      let methodType = '';
      if (curRes['/userController/getTreeByCompany/{departmentID}']) {
        url = `/userController/getTreeByCompany/${getCompanyID()}`;
        methodType = 'GET';
      } else if (curRes['/userController/getTreeByDepart/{departmentID}']) {
        url = `/userController/getTreeByDepart/${getUserDepID()}`;
        methodType = 'GET';
      }
      return requestFunCompon(methodType, url);
    },
    {
      onSuccess: (res) => {
        if (!res?.msgFlag) {
          message.error(res?.msg ?? '获取组织机构失败');
        }
      },
    },
  );

  const reloadLeftTree = () => requestTree.run();

  // const userTreeData = [requestTree.data?.obj];
  const userTreeData = useMemo(() => {
    if (requestTree.data?.msgFlag && requestTree.data?.obj?.id) {
      return [requestTree.data?.obj];
    } else {
      return [];
    }
  }, [requestTree.data]);

  const userTreeLoading = requestTree.loading;

  return {
    reloadLeftTree,
    userTreeData,
    userTreeLoading,
  };
};

export {
  useManageService, //左侧树获取组织机构
};
